#include "../source/tcp_server.hpp"


void OnConnected(const ConnectionPtr &conn)
{
    LOG(LogLevel::DEBUG) << " OnConnected conn.get() : " << conn.get();
}

void OnMessage(const ConnectionPtr &conn, Buffer *buf)
{
    auto len = buf->ReadSize();
    char out[1024] = {0};
    buf->ReadPop(out,len);
    LOG(LogLevel::DEBUG) << " res : " << out;
}
void OnClosed(const ConnectionPtr &conn)
{
    LOG(LogLevel::DEBUG) << " OnClosed conn.get() : " << conn.get();
}

int main()
{
    LoopThread lt;
    TcpClient tc(lt.GetLoop());
    tc.SetClosedCallback(OnClosed);
    tc.SetConnectedCallback(OnConnected);
    tc.SetMessageCallback(OnMessage);
    tc.Connect("127.0.0.1",8989);
    for(int i = 0; i < 20; ++i)
    {
        tc.Send("xzy ninhao !");
        sleep(1);
    }

    
    auto t = lt.GetstdThread();
    tc.ShutDwon();
    t->detach();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return 0;
}